#include "widget.h"
#include "ui_widget.h"
#include <QPaintEvent>
#include <QPushButton>
#include <QSound>
#include <QTimer>
#include <QMediaPlayer>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


    //使用定时器
    timer=new QTimer();
    //定时器有timeout超时信号
    //信号槽(信号和一个对应的函数关联起来）
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
    this->resize(1000,600);
    //初始化蛇身
    QRectF rect(300,180,nodeWidth,nodeHeight);
    snake.append(rect);
    addTop();
    addTop();
    //初始化奖品
    addNewReward();
    setWindowTitle("贪吃蛇游戏界面");
    QSound *startsound=new QSound("D:\\qtthings\\yinyue.wav",this);
        startsound->play();
        startsound->setLoops(-1);

}

Widget::~Widget()
{
    delete ui;
}
//控制方向
void Widget::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_Up:
       if(MoveFlag!=DIR_DOWN){
       MoveFlag=DIR_UP;
       }
       break;
    case Qt::Key_Down:
        if(MoveFlag!=DIR_UP){
        MoveFlag=DIR_DOWN;
        }
       break;
    case Qt::Key_Right:
        if(MoveFlag!=DIR_LEFT){
        MoveFlag=DIR_RIGHT;
        }
       break;
    case Qt::Key_Left:
        if(MoveFlag!=DIR_RIGHT){
        MoveFlag=DIR_LEFT;
        }
       break;
    case Qt::Key_Space:
        if(gameStart==false)
        {
            gameStart=true;
            //start timer
            timer->start(time);

        }
        else{
            gameStart=false;
            timer->stop();
        }
       break;
     default:
        break;
    }
}




void Widget::timeout()
{
    int count=1;
    if(snake[0].intersects(rewardNode))
    {
        count++;
        addNewReward();
    }
    while(count--)
    {
    switch(MoveFlag){
      case DIR_UP:
          addTop();
        break;
      case DIR_DOWN:
          addDown();
        break;
      case DIR_LEFT:
          addLeft();
        break;
      case DIR_RIGHT:
          addRight();
        break;
     default:
        break;
    }
 }
    deleteLast();
    update();
}

void Widget::addTop()
{
    QPointF leftTop;
    QPointF rightBotom;

    if(snake[0].y()-nodeHeight<0){
        leftTop=QPointF(snake[0].x(),this->height()-nodeHeight);
        rightBotom=QPointF(snake[0].x()+nodeWidth,this->height());
    }else {
        leftTop=QPointF(snake[0].x(),snake[0].y()-nodeHeight);
        rightBotom=snake[0].topRight();
}

    snake.insert(0,QRectF(leftTop,rightBotom));

}
void Widget::addDown()
{
    QPointF leftTop;
    QPointF rightBotom;
    if(snake[0].y()+nodeHeight+nodeHeight>this->height())
    {
        leftTop=QPointF(snake[0].x(),0);
        rightBotom=QPointF(snake[0].x()+nodeWidth,nodeHeight);
    }else
    {
        leftTop=snake[0].bottomLeft();
        rightBotom=snake[0].bottomRight()+QPointF(0,nodeHeight);
}
     snake.insert(0,QRectF(leftTop,rightBotom));

 }
void Widget::addLeft()
{
    QPointF leftTop;
    QPointF  rightBottom;

    if(snake[0].x()-nodeWidth<0)
    {
        leftTop=QPointF(this->width()-nodeWidth,snake[0].y());
    }else{
        leftTop=snake[0].topLeft()-QPointF(nodeWidth,0);
}
    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));

}
void Widget::addRight()
{
    QPointF leftTop;
    QPointF rightBottom;

    if(snake[0].x()+nodeWidth*2>this->width())
    {
          leftTop=QPointF(0,snake[0].y());
    }else{
        leftTop=snake[0].topRight();
    }
    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));
}
void Widget::paintEvent(QPaintEvent *event){
    QPainter  painter(this);
    QPen pen;
    QBrush brush;
    QFont font("方正舒体",12,QFont::ExtraLight,false);
    //背景
    QPixmap pix;
    pix.load("D:/qtthings/beijing");
    painter.drawPixmap(0,0,1000,600,pix);


    //画蛇
    pen.setColor(Qt::black);
    brush.setColor(Qt::red);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);

    for(int i=0;i<snake.length();i++)
    {
        painter.drawRect(snake[i]);
    }

    //draw
    pen.setColor(Qt::red);
    brush.setColor(Qt::darkMagenta);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(rewardNode);

    if(checkContact()==1)
    {
        QFont font("方正舒体",30,QFont::ExtraLight,false);
        painter.setFont(font);
        painter.drawText((this->width()-500)/2,(this->height()-300)/2,QString("GAME OVER"));
        timer->stop();
    }
    QWidget::paintEvent(event);
    pen.setColor(Qt::white);
        painter.setPen(pen);
        painter.setFont(font);
        painter.drawText(20,20,QString("当前得分：")+QString("%1").arg(snake.length()));



}

void Widget::deleteLast()
{
    snake.removeLast();
}//通过在蛇头添加新物块在蛇尾减去物块来实现移动

void Widget::addNewReward()
{
    rewardNode=QRectF(qrand()%(this->width()/20)*20,qrand()%(this->height()/20)*20,nodeWidth,nodeHeight);
}//增加新的食物

int Widget::checkContact()
{
    int keck=0;
    for(int i=1;i<snake.length();i++)
    for(int j=i+1;j<snake.length();j++)
    {

        if(snake[i]==snake[j])
        {
            keck=1;
            break;
        }
    }
    return keck;
}//判断是否会吃到自身


